package s19.ch22.ex3;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main implements ActionListener {

	private static String labelPrefix = "Number of button clicks: ";
	private int numClicks = 0;
	final JLabel label = new JLabel(labelPrefix + "0");

	// Specify the look and feel to use. Valid values:
	// null (use the default), "Metal", "System", "Motif", "GTK+"
	final static String LOOKANDFEEL = null;

	public JComponent getJPanelAsJComponent() {

		JButton button = new JButton("I'm a Swing button!");
		button.addActionListener(this);

		// Create a second button
		JButton button2 = new JButton("I'm 2nd Swing button!");
		button2.addActionListener(this);

		JPanel pane = new JPanel(new GridLayout(0, 1));
		pane.add(button);
		pane.add(button2);
		pane.add(label);
		pane.setBorder(BorderFactory.createEmptyBorder(30, // top
				30, // left
				10, // bottom
				30) // right
		);

		return pane;
	}

	public void actionPerformed(ActionEvent e) {
		numClicks++;
		label.setText(labelPrefix + numClicks);
	}

	private static void createAndShowGUI() {

		JFrame.setDefaultLookAndFeelDecorated(true);

		JFrame frame = new JFrame("SwingApplication");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Main app = new Main();
		JComponent contents = app.getJPanelAsJComponent();
		frame.add(contents, BorderLayout.CENTER);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		createAndShowGUI();
	}
}